package com.mj.ipaddress.service;

import java.util.ArrayList;
import java.util.List;

import com.mj.ipaddress.util.HttpUtils;
import com.mj.ipaddress.util.RegUtil;
/**
 * IP地址查询
 * @author zhaominglei
 * @date 2015-3-12
 * 
 */
public class IPaddressService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = IPaddressService.class.getSimpleName();
	private static final String IP138_URL = "http://www.ip138.com/ips138.asp";
	
	public List<String> getIPaddressInfo(String ipaddress) {
		String queryURL = IP138_URL+"?ip="+HttpUtils.encodeURI(ipaddress)+"&action=2";
		String html = HttpUtils.doGetForIP138(queryURL, "gb2312");
		if (html != null && !html.equals("")) {
			List<String> list = new ArrayList<String>();
			String ulStr = RegUtil.getMatchRegStr(html, "<ul class=\"ul1\">(.+?)</ul>");
			String[] contentArray = ulStr.split("</li><li>");
			for (int i = 0; (contentArray != null && contentArray.length > 0) && i < contentArray.length; i++) {
				String content = contentArray[i].replaceAll("<li>", "").replaceAll("</li>", "");
//				content = content.replaceAll("本站", "ip138");
				list.add(content);
			}
			return list;
		}
		return null;
	}
}
